<?php 
namespace Home\Controller;
class FuwuController extends CommonController{
	
	protected $m;
	protected $mm;
	
	function _initialize() {
		parent::_initialize();
		$this->m = D('YgclTeacherContents');
		$this->mm = D('MaterialImages');
	}
	
	public function index(){
		$data = $this->getData();
		$this->info = $data;
		$this->display();
	}
	
	public function getData(){
		$xiaoqus = M('YgclDistrict')->where(array('status'=>1))->order('listorder desc,id desc')->select();
		foreach ($xiaoqus as $key=>$xq){
			$data[$key]['xiaoqu'] = $xq['title'];
			$data[$key]['xqid'] = $xq['id'];
			$uids = M('UsersDistrictRelation')->where(array('did'=>$xq['id']))->getField('uid',true);
			$uids = array_unique($uids);
			$data[$key]['teachers'] = $this->getTeachers($uids); 
		}
		return $data;
	}
	
	public function getTeachers($uids){
		if($uids){
			$data =D('Users')->where(array('uid'=>array('in',$uids)))->relation(true)->select();
		}
		return $data;
	}
	
	public function getkecheng($uid,$did){
		$cids = M('UsersDistrictRelation')->where(array('uid'=>$uid,'did'=>$did))->getField('cid',true);
		if($cids){
			$class = M('YgclClass')->where(array('id'=>array('in',$cids)))->getField('title',true);
			if($class){
				$html = implode('<br/>',$class);
			}
		}
		return $html;
	}
	
	public function detail(){
		$uid = I('uid');
		$did = I('did');
		$info['teacher'] = D('Users')->where(array('uid'=>$uid))->relation(true)->find();
		$info['kecheng'] = $this->getkecheng($uid, $did);
		$this->info = $info;
		$this->user = $_SESSION['_loginUser_home'];
		$this->display();
	}
	
	Public function getjiaoshitiandi(){
		$uid = I('uid');
		$page = I('page',1);
		$info = D('YgclTeacherContents')->where(array('status'=>1,'creator_id'=>$uid))->order('id desc')->relation(true)->page($page,10)->select();
		foreach ($info as $key=>$if){
			$d = splitName($if['users']['realname']);
			$info[$key]['realname'] = $d[0];
			
			//判断用户是否点赞
			$uid = $_SESSION['_loginUser_home']['uid'];
			$bool = D('YgclDianzai','Service')->get_isdianzai($uid,$if['id'],'teacher');
			$bool ? $info[$key]['dianzai'] = 1:$info[$key]['dianzai']=0;
			
			//判断用户是否打赏
			$dashang = D('YgclOrder','Service')->get_isdashang($uid,'dashang',$if['id']);
			$dashang ?$info[$key]['dashang']=1:$info[$key]['dashang']=0;
		}
// 		dump($info);
		count($info) < 10 ? $next = 'no':$next = 'yes';
		$data = array(
				'next'=>$next,
				'info'=>$info,
		);
		$this->ajaxReturn($data);
	}
	
	public function test_submit(){
		$content = $_POST['content'];
		$media_id = $_POST['media_id'];
		
		if(!$content && !$media_id){
			$this->error('请先填写内容或上传图片');
			exit();
		}
		
		$data['desc'] = $content;
		$data['creator_id'] = $_SESSION['_loginUser_home']['uid'];
		$data['create_time'] = Date('Y-m-d H:i:s');
		$data['update_time'] = Date('Y-m-d H:i:s');
		$id = $this->m->add($data);
			
		if($media_id){
			import('@.Lib.MiniWx.wechat');
			$weObj = new \Wechat(C('WEIXIN_CONFIG')); //创建实例对象
			$access_token = $weObj->checkAuth();
			$dir = '/Uploads/'.Date('Y_m_d').'/';//为方便管理图片 保存图片时 已时间作一层目录作区分
			//创建文件夹
			_createFolder('.'.$dir);
			foreach($_REQUEST['media_id'] as $mid){
					
				$url = 'http://file.api.weixin.qq.com/cgi-bin/media/get?access_token='.$access_token.'&media_id='.$mid;
			
				$time = time().substr($mid, 9,3);
				$filename = 'wx_'.$time.'.jpg';   //定义图片文件名
				$Http = new \Org\Net\Http;
				if($Http::curlDownload($url,'.'.$dir.$filename)){//http下载图片
					//$this->success('提交成功',U('index'));
			
				}else{
					$imgpath = $dir.$filename;
					$w = array(
							'fk_id'=>$id,
							'fk_group' => 'content',
							'url' => $imgpath,
							'create_time' => Date('Y-m-d H:i:s')
					);
					$this->mm->add($w);
					//echo '<img src="'.$imgpath.'" height="500">';
					//echo 'success<br>';
				}
			}
		}
		$this->success('提交成功');
	}
	
}
?>